﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Enums;

namespace BuildPlannerLib.SimEvents
{
    // Derived class from Event for a worker reaching far mineral patches
    public class ReachFarMinerals : SimEvent
    {
        // Field for the base at which mining is taking place
        public Base AtBase;

        // Constructor with parameters for parent build, base, and time
        public ReachFarMinerals(Build build, Base b, int time)
        {
            Build = build;
            AtBase = b;
            Time = time;
        }

        // If there is an idle mineral patch, mines from it.  Otherwise, sets the worker to the Idle category.
        public override void Execute()
        {
            AtBase.MineralWorkerStatus[1]--;
            MineralFlags mineralflags;
            int newTime = Time;

            if (AtBase.IdleMineralPatches[3] > 0)
            {
                mineralflags = MineralFlags.GoldPatch & MineralFlags.ClosePatch;
                newTime += 294;
                AtBase.IdleMineralPatches[3]--;
                Build.EventList.Add(new FinishMineralMining(Build, AtBase, mineralflags, newTime));
            }
            else if (AtBase.IdleMineralPatches[1] > 0)
            {
                mineralflags = MineralFlags.GoldPatch;
                newTime += 294;
                AtBase.IdleMineralPatches[1]--;
                Build.EventList.Add(new FinishMineralMining(Build, AtBase, mineralflags, newTime));
            }
            else if (AtBase.IdleMineralPatches[2] > 0)
            {
                mineralflags = MineralFlags.ClosePatch;
                newTime += 294;
                AtBase.IdleMineralPatches[2]--;
                Build.EventList.Add(new FinishMineralMining(Build, AtBase, mineralflags, newTime));
            }
            else if (AtBase.IdleMineralPatches[0] > 0)
            {
                mineralflags = MineralFlags.FarPatch;
                newTime += 294;
                AtBase.IdleMineralPatches[0]--;
                Build.EventList.Add(new FinishMineralMining(Build, AtBase, mineralflags, newTime));
            }
            else
            {
                AtBase.MineralWorkerStatus[2]++;
            }
        }

        public override Base GetBase()
        {
            return AtBase;
        }
    }
}